package com.ylkj.wx.utils.zxing;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class ZXingQRCodeUtil {
	
	public static void creatQRCode() {  
		 //定义二维码的样式  
        int width = 300;  
        int height = 300;  
        String format = "png";  
        String contents = "http://blog.csdn.net/qq_30507287";//扫描二维码时产生的连接  
          
        //定义二维码的参数  
        HashMap hints = new HashMap();  
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");  
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);//设置二维码的容错等级  
        hints.put(EncodeHintType.MARGIN, 2);//边距  
          
        try {  
            BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints);  
            Path file = new File("D:/pay/alipaydirect/RSA/create_direct_pay_by_user-JAVA-UTF-8/img.png").toPath();//保存的路径  
            MatrixToImageWriter.writeToPath(bitMatrix, format, file);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
	    
	}  
	
	public static void ReadQRCode(){
		try {  
            MultiFormatReader formatReader = new MultiFormatReader();  
              
            File file = new File("D:/pay/alipaydirect/img.png");  
            BufferedImage image = ImageIO.read(file);//读取文件，识别成一个图片  
            BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));  
            //二维码的参数  
            HashMap hints = new HashMap();  
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");  
            Result result = formatReader.decode(binaryBitmap, hints);  
              
            System.out.println("解析二维码的结果：" + result.toString());  
            System.out.println("二维码的格式：" + result.getBarcodeFormat());  
            System.out.println("二维码的内容：" + result.getText());  
            System.out.println("二维码的nn：" + result.getResultMetadata());
            System.out.println("二维码的时间戳：" + result.getTimestamp());
            System.out.println("二维码的时间戳：" + result.getResultPoints());
        } catch (NotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
	}
	
	public static void main(String[] args) {  
	   // QRCodeUtil.creatQRCode();  
	    ZXingQRCodeUtil.ReadQRCode();
	    
	} 
}
